package com.neuedu.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.his.mapper.PatientcostsMapper;
import com.neuedu.his.mapper.RegisterMapper;
import com.neuedu.his.po.Patientcosts;
import com.neuedu.his.po.Register;
import com.neuedu.his.service.ChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ChargeServiceImpl implements ChargeService {

    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private PatientcostsMapper patientcostsMapper;

    // 根据病历号查询患者消费信息
    @Override
    public Map<String, Object> getByMid(Integer mid) {
        // 查询患者信息
        QueryWrapper<Register> qw = new QueryWrapper<>();
        qw.eq("casenumber", mid);
        Register reg = registerMapper.selectOne(qw);

        // 查询患者的消费信息
        QueryWrapper<Patientcosts> pcQw = new QueryWrapper<>();
        pcQw.eq("RegistID", reg.getId());
        List<Patientcosts> list = patientcostsMapper.selectList(pcQw);

        // 返回结果
        Map<String, Object> map = new HashMap<>();
        map.put("info", reg);
        map.put("list", list);
        return map;
    }

    // 收费结算
    @Override
    public void chargeAll(Integer mid, Double totalFee) {
        // 查询患者信息
        QueryWrapper<Register> qw = new QueryWrapper<>();
        qw.eq("casenumber", mid);
        Register reg = registerMapper.selectOne(qw);

        // 更新患者消费信息的状态为已收费
        QueryWrapper<Patientcosts> pcQw = new QueryWrapper<>();
        pcQw.eq("RegistID", reg.getId());
        List<Patientcosts> list = patientcostsMapper.selectList(pcQw);
        for (Patientcosts pc : list) {
            pc.setState(2);
            patientcostsMapper.updateById(pc);
        }

        // 生成发票等操作，这里省略
    }
}